this.name        = "dredger salvage missile"; 
this.author      = "eric walch"; 
this.copyright   = "� 2008 Walch & Sabre & Little Bear"; 
this.description = "Missile to initiate salvaging."; 
this.version     = "1.1"; 

this.shipSpawned = function()
{
    this.addPirates = false
}

this.shipDied = function(ship)
{
    if(ship != this.ship.target) return; // killed by someting else than targetship.
    if(worldScripts.deep_space_dredger.checkSalvage(ship)) {this.ship.commsMessage(expandDescription("[salvage_drone_busy]")); return;}
    if(ship.isShip && ship.scanClass == "CLASS_CARGO" && (ship.AI == "nullAI.plist" || ship.AI == "dumbAI.plist"))
    {
        if (Math.random() < 0.05 || ship.maxSpeed < 50 || ship.script.dammaged)
        {
            this.ship.commsMessage(expandDescription("[salvage_drone_damaged]"));
			ship.script.dammaged = true;
            return; // Only process script added derelicts. (others won't work as maxSpeed = 0)
        }
        if(ship.mass < 1E6) // Hamadryad has 985 456 kg, anaconda has 429 553 kg, a sidewinder 26 737 kg
        {
            // only add when no other dredgers nearby
            if(system.shipsWithPrimaryRole("dredgers", ship, 300E3).length == 0)
            {
                system.legacy_addShipsAtPrecisely("dredgers", 1, "abs", player.ship.position.add(Vector3D.randomDirection().multiply(200E3)));
                this.addPirates = true;  // only add once per added dredger.
            }
            var dredger = system.shipsWithPrimaryRole("dredgers")[0]; // choose the closest
            ship.target = dredger;
            ship.switchAI("dredgersDockingAI.plist")
            ship.primaryRole = "trader" // Make sure pirates will find it
			if(ship.displayName.indexOf("(derelict)")) ship.displayName = ship.displayName.replace(" (derelict)", "") 
            worldScripts.deep_space_dredger.addSalvage(ship)
            ship.name = ship.displayName // Store the original displayname in case someone altered it.
            ship.displayName = ship.displayName + " (auto pilot)" // Make sure it stands out among other ships
            dredger.target = ship
            dredger.AIState = "HEAD_FOR_SALVAGE"
			ship.script.shipDied = worldScripts.deep_space_dredger.derelictDied; //script attached to derelict ship
			ship.script.shipDockedWithStation = worldScripts.deep_space_dredger.derelictDocked; //script attached to derelict ship
            if(this.addPirates)
            {
                var dredgerDirection = dredger.position.subtract(ship.position) // Vector from derelict to dredger, vector lenghth is distance.
                system.legacy_addShipsAtPrecisely("pirate", Math.ceil(Math.random()*5), "abs", ship.position.add(dredgerDirection.multiply(0.25)))
                system.legacy_addShipsAtPrecisely("pirate", Math.ceil(Math.random()*5), "abs", ship.position.add(dredgerDirection.multiply(0.40)))
                var pirates = system.shipsWithPrimaryRole("pirate", dredger, 300E3);
                for (var i=0; i<pirates.length;i++)
                {
                    if(pirates[i].bounty == 0 && (clock.absoluteSeconds - pirates[i].spawnTime) < 5)
                    pirates[i].bounty = Math.ceil(Math.random()*50)
                }
            }
            if(dredger.displayName == dredger.name)
                dredger.displayName = expandDescription("[salvage_drone_gotit11] [salvage_drone_gotit12]")
            this.ship.commsMessage(expandDescription("[salvage_drone_gotita]") + dredger.displayName + expandDescription("[salvage_drone_gotitb]"))
            // take note that above code runs before any ship-JS of the dredger itself.
        }
        else  this.ship.commsMessage(expandDescription("[salvage_drone_toobig]"))
    }
    else this.ship.commsMessage(expandDescription("[salvage_drone_notwreak]"))
}